package cn.woblog.recyclerviewsample;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.transition.Slide;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.woblog.recyclerviewsample.CardViewActivity.MyAdapter.MyViewHolder;
import cn.woblog.recyclerviewsample.listener.OnItemClickListener;
import cn.woblog.recyclerviewsample.listener.OnItemLongClickListener;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import java.util.ArrayList;
import java.util.List;

public class CardViewActivity extends AppCompatActivity implements OnItemClickListener,
    OnItemLongClickListener {

  private List<String> data = new ArrayList<>();

  public static List<String> getTestImage() {
    ArrayList<String> strings = new ArrayList<>();
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037235_3453.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037235_9280.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037234_3539.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037234_6318.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037194_2965.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037193_1687.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037193_1286.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037192_8379.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037178_9374.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037177_1254.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037177_6203.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037152_6352.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037151_9565.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037151_7904.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037148_7104.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037129_8825.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037128_5291.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037128_3531.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037127_1085.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037095_7515.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037094_8001.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037093_7168.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037091_4950.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949643_6410.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949642_6939.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949630_4505.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949630_4593.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949629_7309.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949629_8247.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949615_1986.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949614_8482.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949614_3743.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949614_4199.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949599_3416.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949599_5269.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949598_7858.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949598_9982.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949578_2770.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949578_8744.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949577_5210.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949577_1998.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949482_8813.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949481_6577.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949480_4490.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949455_6792.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949455_6345.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949442_4553.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949441_8987.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949441_5454.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949454_6367.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949442_4562.j");
    return strings;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_card_view);

    data.addAll(getTestImage());

    RecyclerView rv = (RecyclerView) findViewById(R.id.rv);

//如果你知道你的RecyclerView的高度在使用期间不会变化
// 使用此设置可以提高性能，后面会相应的文章分析为什么
    rv.setHasFixedSize(true);

//设置一个布局管理器
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    rv.setLayoutManager(linearLayoutManager);

//设置适配器
    MyAdapter myAdapter = new MyAdapter(this);
    rv.setAdapter(myAdapter);
    myAdapter.setOnItemClickListener(this);
    myAdapter.setOnItemLongClickListener(this);
  }

  @Override
  public void onItemClick(ViewHolder viewHolder, int position) {
    MyViewHolder holder = (MyViewHolder) viewHolder;

    Slide slide = new Slide();
    getWindow().setEnterTransition(slide);
    getWindow().setExitTransition(slide);

    ActivityOptionsCompat options = ActivityOptionsCompat
        .makeSceneTransitionAnimation(this, holder.iv, "iv");

    String s = data.get(position);
    Intent intent = new Intent(this, NewsDetailActivity.class);
    intent.putExtra(NewsDetailActivity.URL, s);
    startActivity(intent, options.toBundle());
  }

  @Override
  public boolean onItemLongClick(ViewHolder holder, int position) {
    Toast.makeText(this, "Long Click", Toast.LENGTH_SHORT).show();
    return true;
  }

  class MyAdapter extends RecyclerView.Adapter<CardViewActivity.MyAdapter.MyViewHolder> {

    private final LayoutInflater layoutInflater;
    private int[] heights = new int[data.size()];
    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;

    public MyAdapter(Context context) {
      layoutInflater = LayoutInflater.from(context);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
      this.onItemLongClickListener = onItemLongClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
      this.onItemClickListener = onItemClickListener;
    }

    @Override
    public CardViewActivity.MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
        int viewType) {
      return new MyAdapter.MyViewHolder(
          layoutInflater.inflate(R.layout.item_card_view, parent, false));
    }

    @Override
    public void onBindViewHolder(final CardViewActivity.MyAdapter.MyViewHolder holder,
        final int position) {
      String d = CardViewActivity.this.data.get(position);
      holder.bindData(d, position);
      if (onItemClickListener != null) {
        holder.itemView.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
            onItemClickListener.onItemClick(holder, position);
          }
        });
        if (onItemLongClickListener != null) {
          holder.itemView.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
              return onItemLongClickListener.onItemLongClick(holder, position);
            }
          });
        }
      }
    }

    @Override
    public int getItemCount() {
      return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

      private final TextView tv;
      private final ImageView iv;

      public MyViewHolder(View itemView) {
        super(itemView);
        tv = (TextView) itemView.findViewById(R.id.tv);
        iv = (ImageView) itemView.findViewById(R.id.iv);
      }

      public void bindData(String d, final int position) {
//        tv.setText(d);
//        Glide.with(CardViewActivity.this).load(d).diskCacheStrategy(DiskCacheStrategy.ALL)
//            .into(iv);

//如果有高度，就直接取出来不用再计算了
        final Integer height = heights[position];
        if (height == 0) {
          //没有高度，需要计算
          Glide.with(CardViewActivity.this).load(d).diskCacheStrategy(DiskCacheStrategy.ALL)
              .into(new SimpleTarget<GlideDrawable>() {
                @Override
                public void onResourceReady(GlideDrawable resource,
                    GlideAnimation<? super GlideDrawable> glideAnimation) {
                  Log.d("TAG", iv.getWidth() + "," + resource.getIntrinsicWidth());
                  //计算ImageView的高度
                  int imageWidth = resource.getIntrinsicWidth();
                  int imageHeight = resource.getIntrinsicHeight();
                  int imageViewWidth = iv.getWidth();

                  double scale = imageWidth * 1.0 / imageViewWidth;
                  LayoutParams layoutParams = iv.getLayoutParams();
                  int h = (int) (imageHeight / scale);
                  layoutParams.height = h;
                  iv.setLayoutParams(layoutParams);
                  iv.setImageDrawable(resource);

                  heights[position] = h;
                }
              });
        } else {
          //已经计算了，直接拿出来用
          LayoutParams layoutParams = iv.getLayoutParams();
          layoutParams.height = height;
          iv.setLayoutParams(layoutParams);

          Glide.with(CardViewActivity.this).load(d).diskCacheStrategy(DiskCacheStrategy.ALL)
              .into(iv);
        }


      }
    }


  }
}
